using System;
using System.Collections;
using System.Windows.Forms;
using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for ItemsForms.
	/// </summary>
	public class ProductsForm : System.Windows.Forms.Form, IListViewForm
	{
		private System.Windows.Forms.ColumnHeader columnHeader_Key;
		private System.Windows.Forms.ColumnHeader columnHeader_Name;
		private System.Windows.Forms.ColumnHeader columnHeader_Amount;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ToolBar toolBar;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.ToolBarButton toolBarButton_New;
		private System.Windows.Forms.ToolBarButton toolBarButton_Delete;
		private System.Windows.Forms.ToolBarButton toolBarButton_Edit;
		private System.Windows.Forms.ListView listView;
		public System.Windows.Forms.ToolBarButton toolBarButton_Plus;
		public System.Windows.Forms.ToolBarButton toolBarButton_Minus;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cb_categories;
		public System.Windows.Forms.ToolBarButton toolBarButton_reusual;
		private System.Windows.Forms.ContextMenu contextMenu;
		private System.Windows.Forms.MenuItem ctxMenu_Add;
		private System.Windows.Forms.MenuItem ctxMenu_Subtract;
		private System.Windows.Forms.MenuItem ctxMenu_Edit;
		private System.Windows.Forms.ColumnHeader columnHeader_Price;

		private ProducstListFormManager listFormManager;
				
		#region Constructor
		public ProductsForm()
		{
			InitializeComponent();

			// A workaround WM2003 SE Toolbar bug
			toolBar.ImageList = imageList;
			toolBar.Buttons[0].ImageIndex = 0;
			toolBar.Buttons[1].ImageIndex = 1;
			toolBar.Buttons[2].ImageIndex = 2;
			toolBar.Buttons[3].ImageIndex = 3;
			toolBar.Buttons[4].ImageIndex = 4;
			toolBar.Buttons[5].ImageIndex = 5;


			listFormManager = new ProducstListFormManager(this, listView, 
														  Database.Instance.Categories);

			listFormManager.CategoryChanged+=new EventHandler(listFormManager_CategoryChanged);
		}
		#endregion

		private const int	nameColumnId = 1;
		

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ProductsForm));
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeader_Key = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Name = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Price = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Amount = new System.Windows.Forms.ColumnHeader();
			this.contextMenu = new System.Windows.Forms.ContextMenu();
			this.ctxMenu_Add = new System.Windows.Forms.MenuItem();
			this.ctxMenu_Subtract = new System.Windows.Forms.MenuItem();
			this.ctxMenu_Edit = new System.Windows.Forms.MenuItem();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.toolBar = new System.Windows.Forms.ToolBar();
			this.toolBarButton_New = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Delete = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Edit = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Plus = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Minus = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_reusual = new System.Windows.Forms.ToolBarButton();
			this.imageList = new System.Windows.Forms.ImageList();
			this.label1 = new System.Windows.Forms.Label();
			this.cb_categories = new System.Windows.Forms.ComboBox();
			// 
			// listView
			// 
			this.listView.Columns.Add(this.columnHeader_Key);
			this.listView.Columns.Add(this.columnHeader_Name);
			this.listView.Columns.Add(this.columnHeader_Price);
			this.listView.Columns.Add(this.columnHeader_Amount);
			this.listView.ContextMenu = this.contextMenu;
			this.listView.Enabled = ((bool)(resources.GetObject("listView.Enabled")));
			this.listView.FullRowSelect = true;
			this.listView.Location = ((System.Drawing.Point)(resources.GetObject("listView.Location")));
			this.listView.Size = ((System.Drawing.Size)(resources.GetObject("listView.Size")));
			this.listView.Text = resources.GetString("listView.Text");
			this.listView.View = System.Windows.Forms.View.Details;
			this.listView.Visible = ((bool)(resources.GetObject("listView.Visible")));
			this.listView.ItemActivate += new System.EventHandler(this.listView_ItemActivate);
			this.listView.SelectedIndexChanged += new System.EventHandler(this.listView_SelectedIndexChanged);
			// 
			// columnHeader_Key
			// 
			this.columnHeader_Key.Text = resources.GetString("columnHeader_Key.Text");
			this.columnHeader_Key.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Key.TextAlign")));
			this.columnHeader_Key.Width = ((int)(resources.GetObject("columnHeader_Key.Width")));
			// 
			// columnHeader_Name
			// 
			this.columnHeader_Name.Text = resources.GetString("columnHeader_Name.Text");
			this.columnHeader_Name.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Name.TextAlign")));
			this.columnHeader_Name.Width = ((int)(resources.GetObject("columnHeader_Name.Width")));
			// 
			// columnHeader_Price
			// 
			this.columnHeader_Price.Text = resources.GetString("columnHeader_Price.Text");
			this.columnHeader_Price.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Price.TextAlign")));
			this.columnHeader_Price.Width = ((int)(resources.GetObject("columnHeader_Price.Width")));
			// 
			// columnHeader_Amount
			// 
			this.columnHeader_Amount.Text = resources.GetString("columnHeader_Amount.Text");
			this.columnHeader_Amount.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Amount.TextAlign")));
			this.columnHeader_Amount.Width = ((int)(resources.GetObject("columnHeader_Amount.Width")));
			// 
			// contextMenu
			// 
			this.contextMenu.MenuItems.Add(this.ctxMenu_Add);
			this.contextMenu.MenuItems.Add(this.ctxMenu_Subtract);
			this.contextMenu.MenuItems.Add(this.ctxMenu_Edit);
			// 
			// ctxMenu_Add
			// 
			this.ctxMenu_Add.Enabled = ((bool)(resources.GetObject("ctxMenu_Add.Enabled")));
			this.ctxMenu_Add.Text = resources.GetString("ctxMenu_Add.Text");
			this.ctxMenu_Add.Click += new System.EventHandler(this.ctxMenu_Add_Click);
			// 
			// ctxMenu_Subtract
			// 
			this.ctxMenu_Subtract.Enabled = ((bool)(resources.GetObject("ctxMenu_Subtract.Enabled")));
			this.ctxMenu_Subtract.Text = resources.GetString("ctxMenu_Subtract.Text");
			this.ctxMenu_Subtract.Click += new System.EventHandler(this.ctxMenu_Subtract_Click);
			// 
			// ctxMenu_Edit
			// 
			this.ctxMenu_Edit.Enabled = ((bool)(resources.GetObject("ctxMenu_Edit.Enabled")));
			this.ctxMenu_Edit.Text = resources.GetString("ctxMenu_Edit.Text");
			this.ctxMenu_Edit.Click += new System.EventHandler(this.ctxMenu_Edit_Click);
			// 
			// toolBar
			// 
			this.toolBar.Buttons.Add(this.toolBarButton_New);
			this.toolBar.Buttons.Add(this.toolBarButton_Delete);
			this.toolBar.Buttons.Add(this.toolBarButton_Edit);
			this.toolBar.Buttons.Add(this.toolBarButton_Plus);
			this.toolBar.Buttons.Add(this.toolBarButton_Minus);
			this.toolBar.Buttons.Add(this.toolBarButton_reusual);
			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
			// 
			// toolBarButton_New
			// 
			this.toolBarButton_New.Enabled = ((bool)(resources.GetObject("toolBarButton_New.Enabled")));
			this.toolBarButton_New.ImageIndex = ((int)(resources.GetObject("toolBarButton_New.ImageIndex")));
			this.toolBarButton_New.Visible = ((bool)(resources.GetObject("toolBarButton_New.Visible")));
			// 
			// toolBarButton_Delete
			// 
			this.toolBarButton_Delete.Enabled = ((bool)(resources.GetObject("toolBarButton_Delete.Enabled")));
			this.toolBarButton_Delete.ImageIndex = ((int)(resources.GetObject("toolBarButton_Delete.ImageIndex")));
			this.toolBarButton_Delete.Visible = ((bool)(resources.GetObject("toolBarButton_Delete.Visible")));
			// 
			// toolBarButton_Edit
			// 
			this.toolBarButton_Edit.Enabled = ((bool)(resources.GetObject("toolBarButton_Edit.Enabled")));
			this.toolBarButton_Edit.ImageIndex = ((int)(resources.GetObject("toolBarButton_Edit.ImageIndex")));
			this.toolBarButton_Edit.Visible = ((bool)(resources.GetObject("toolBarButton_Edit.Visible")));
			// 
			// toolBarButton_Plus
			// 
			this.toolBarButton_Plus.Enabled = ((bool)(resources.GetObject("toolBarButton_Plus.Enabled")));
			this.toolBarButton_Plus.ImageIndex = ((int)(resources.GetObject("toolBarButton_Plus.ImageIndex")));
			this.toolBarButton_Plus.Visible = ((bool)(resources.GetObject("toolBarButton_Plus.Visible")));
			// 
			// toolBarButton_Minus
			// 
			this.toolBarButton_Minus.Enabled = ((bool)(resources.GetObject("toolBarButton_Minus.Enabled")));
			this.toolBarButton_Minus.ImageIndex = ((int)(resources.GetObject("toolBarButton_Minus.ImageIndex")));
			this.toolBarButton_Minus.Visible = ((bool)(resources.GetObject("toolBarButton_Minus.Visible")));
			// 
			// toolBarButton_reusual
			// 
			this.toolBarButton_reusual.Enabled = ((bool)(resources.GetObject("toolBarButton_reusual.Enabled")));
			this.toolBarButton_reusual.ImageIndex = ((int)(resources.GetObject("toolBarButton_reusual.ImageIndex")));
			this.toolBarButton_reusual.Visible = ((bool)(resources.GetObject("toolBarButton_reusual.Visible")));
			// 
			// imageList
			// 
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource3"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource4"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource5"))));
			this.imageList.ImageSize = ((System.Drawing.Size)(resources.GetObject("imageList.ImageSize")));
			// 
			// label1
			// 
			this.label1.Enabled = ((bool)(resources.GetObject("label1.Enabled")));
			this.label1.Font = ((System.Drawing.Font)(resources.GetObject("label1.Font")));
			this.label1.Location = ((System.Drawing.Point)(resources.GetObject("label1.Location")));
			this.label1.Size = ((System.Drawing.Size)(resources.GetObject("label1.Size")));
			this.label1.Text = resources.GetString("label1.Text");
			this.label1.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("label1.TextAlign")));
			this.label1.Visible = ((bool)(resources.GetObject("label1.Visible")));
			// 
			// cb_categories
			// 
			this.cb_categories.DisplayMember = "Name";
			this.cb_categories.Enabled = ((bool)(resources.GetObject("cb_categories.Enabled")));
			this.cb_categories.Font = ((System.Drawing.Font)(resources.GetObject("cb_categories.Font")));
			this.cb_categories.Location = ((System.Drawing.Point)(resources.GetObject("cb_categories.Location")));
			this.cb_categories.Size = ((System.Drawing.Size)(resources.GetObject("cb_categories.Size")));
			this.cb_categories.Text = resources.GetString("cb_categories.Text");
			this.cb_categories.ValueMember = "Key";
			this.cb_categories.Visible = ((bool)(resources.GetObject("cb_categories.Visible")));
			this.cb_categories.SelectedIndexChanged += new System.EventHandler(this.cb_categories_SelectedIndexChanged);
			// 
			// ProductsForm
			// 
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.cb_categories);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.listView);
			this.Controls.Add(this.toolBar);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.MinimizeBox = false;
			this.Text = resources.GetString("$this.Text");
			this.Visible = ((bool)(resources.GetObject("$this.Visible")));
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ProductsForm_Closing);
			this.Load += new System.EventHandler(this.ItemsForm_Load);

		}
		#endregion

		#region Private Event Handler
		private void listFormManager_CategoryChanged(object sender, EventArgs e)
		{
			cb_categories.SelectedValue = listFormManager.CurrentCategoryKey;
		}
		#endregion

		#region IListViewForm Members

		public ToolBarButton DeleteButton
		{
			get
			{
				return toolBarButton_Delete;
			}
		}

		public ToolBarButton EditButton
		{
			get
			{
				return toolBarButton_Edit;
			}
		}

		public ToolBarButton NewButton
		{
			get
			{
				return toolBarButton_New;
			}
		}

		public ToolBarButton UpButton
		{
			get
			{
				return null;
			}
		}

		public ToolBarButton DownButton
		{
			get
			{
				return null;
			}
		}

		public IItemPropertiesForm CreatePropertiesForm(ItemBase item)
		{
			return new ProductPropertiesForm((Product)item);
		}

		#endregion

		#region Form Event Handlers
		private void ItemsForm_Load(object sender, System.EventArgs e)
		{
			ArrayList tempArray = (ArrayList)Database.Instance.Categories.List.Clone();
			// Create dummy "all" category
			Category category = new Category(LangResources.Instance.Resources.GetString("allKey"));
			category.Key = new Key(-1);
			// Add it to the list
			tempArray.Insert(0, category);
			cb_categories.DataSource = tempArray;
		}

		private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			listFormManager.ToolbarEventsHandler(e);
		}

		private void listView_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			listFormManager.EnableDisableItems();
		}

		private void listView_ItemActivate(object sender, System.EventArgs e)
		{
			listFormManager.HandleEditItemEvent();
		}

		private void cb_categories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			listFormManager.CurrentCategoryKey = (Key)cb_categories.SelectedValue;
			listFormManager.FillListView();
		}

		private void ProductsForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			listFormManager.UpdateDatabase();
		}

		private void ctxMenu_Add_Click(object sender, System.EventArgs e)
		{
			listFormManager.ToolbarEventsHandler(new ToolBarButtonClickEventArgs(toolBarButton_Plus));
		}

		private void ctxMenu_Subtract_Click(object sender, System.EventArgs e)
		{
			listFormManager.ToolbarEventsHandler(new ToolBarButtonClickEventArgs(toolBarButton_Minus));		
		}

		private void ctxMenu_Edit_Click(object sender, System.EventArgs e)
		{
			listFormManager.ToolbarEventsHandler(new ToolBarButtonClickEventArgs(EditButton));		
		}

		#endregion

	}
}
